﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using TCM.Filters;
using WebMatrix.WebData;

namespace TCM.Controllers
{
     [InitializeSimpleMembership]
    public class UserController : Controller
    {
        private TCMContext db = new TCMContext();

        //
        // GET: /User/

        public ActionResult Index()
        {
            var userProfiles = db.UserProfiles;
            return View(userProfiles.ToList());
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id = 0)
        {
            UserProfile userprofile = db.UserProfiles.Find(id);
            if (userprofile == null)
            {
                return HttpNotFound();
            }
            return View(userprofile);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(UserProfile userprofile)
        {
           
          
            if (ModelState.IsValid)
            {
                try
                {
                    WebSecurity.CreateUserAndAccount(userprofile.UserName, userprofile.Password);
                    return RedirectToAction("Index");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", AccountController.ErrorCodeToString(e.StatusCode));
                }
               
               
                
            }

            return View(userprofile);
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id = 0)
        {
            UserProfile userprofile = db.UserProfiles.Find(id);
            var roles = userprofile.webpages_UsersInRoles.Select(p=>p.RoleId).ToArray();
            userprofile.webpages_Roles1 = roles;
            userprofile.Password = "p@ssword";
            userprofile.ConfirmPassword = "p@ssword";


            if (userprofile == null)
            {
                return HttpNotFound();
            }
            return View(userprofile);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(UserProfile userprofile)
        {
            if (ModelState.IsValid)
            {
                //var arr=;
                int[] v;
                
                v=userprofile.webpages_Roles1.ToArray();

                //ICollection<webpages_Roles> s = db.rowebpages_Roles.Where(p => v.Contains(p.RoleId)).ToList();
                //userprofile.webpages_UsersInRoles = s;
                var t = userprofile.webpages_UsersInRoles;
                foreach (var role_old in t.ToList())
                {
                    db.webpages_UsersInRoles1.Remove(role_old);
                    
                }
                //db.Entry(userprofile).State = EntityState.Modified;
                db.SaveChanges();
               
                foreach (int role in userprofile.webpages_Roles1)
                {
                  
                    db.webpages_UsersInRoles1.Add(new webpages_UsersInRoles1() { RoleId = role, UserId = userprofile.UserId });
                  
                }
                
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(userprofile);
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id = 0)
        {
            UserProfile userprofile = db.UserProfiles.Find(id);
            if (userprofile == null)
            {
                return HttpNotFound();
            }
            return View(userprofile);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            webpages_Membership membership = db.webpages_Membership.Find(id);
            UserProfile userprofile = db.UserProfiles.Find(id);
            db.webpages_Membership.Remove(membership);
            db.UserProfiles.Remove(userprofile);

            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}